package com.hackerrank.challenges.stockmaximize;

import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int T = in.nextInt();
		for(int i = 0; i < T; i++) {
			int N = in.nextInt();
			long p[] = new long[N], m = 0, n = 0, q = 0;
			for(int j = 0; j < N; j++)
				p[j] = in.nextInt();
			boolean b[] = new boolean[N];
			for(int j = N - 1; j >= 0; j--) {
				if(p[j] > m)
					m = p[j];
				else if(p[j] < m)
					b[j] = true;
			}
			for(int j = 0; j < N; j++) {
				if(b[j]) {
					n++;
					q -= p[j];
				} else {
					q += p[j] * n;
					n = 0;
				}
			}
			System.out.println(q);
		}
	}
}
